package com.supermap.mouseListeners;

import com.supermap.Helper;
import com.supermap.consts.AttrConsts;
import com.supermap.data.Maps;
import com.supermap.data.Rectangle2D;
import com.supermap.data.Workspace;
import com.supermap.dialogs.InputNameDialog;
import com.supermap.mapping.ImageType;
import com.supermap.mapping.Map;
import com.supermap.ui.Action;
import com.supermap.ui.MapControl;
import com.supermap.ui.WorkspaceTree;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class MapControlMouseListener implements MouseListener {
    private final MapControl mapControl;
    private final WorkspaceTree workspaceTree;

    public MapControlMouseListener(MapControl mapControl, WorkspaceTree workspaceTree) {
        this.mapControl = mapControl;
        this.workspaceTree = workspaceTree;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        Object source = e.getSource();
        if (!(source instanceof MapControl)) {
            return;
        }

        JPopupMenu popupMenu = mapControl.getComponentPopupMenu();
        if (popupMenu != null) {
            popupMenu.removeAll();
            popupMenu.setVisible(false);
        }

        switch (e.getButton()) {
            // 鼠标左键
            case MouseEvent.BUTTON1:
                break;
            // 鼠标中键
            case MouseEvent.BUTTON2:
                break;
            // 鼠标右键
            case MouseEvent.BUTTON3:
                if (mapControl.getAction() != Action.SELECT2) {
                    mapControl.setAction(Action.SELECT2);
                    return;
                }

                if (popupMenu == null) {
                    popupMenu = new JPopupMenu();
                    mapControl.setComponentPopupMenu(popupMenu);
                    popupMenu.setLocation(e.getLocationOnScreen());
                } else {
                    popupMenu.removeAll();
                    popupMenu.setVisible(false);
                }

                initMapControlContextMenu(popupMenu);
                break;
            default:
                break;
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    /**
     * 初始化地图控件鼠标右键菜单
     *
     * @param popupMenu 鼠标右键菜单对象
     */
    private void initMapControlContextMenu(JPopupMenu popupMenu) {
        JMenuItem selectItem = new JMenuItem("选择");
        selectItem.addActionListener(e -> {
            popupMenu.setVisible(false);
            mapControl.setAction(Action.SELECT2);
        });
        popupMenu.add(selectItem);

        JMenuItem panItem = new JMenuItem("漫游");
        panItem.addActionListener(e -> {
            popupMenu.setVisible(false);
            mapControl.setAction(Action.PAN);
        });
        popupMenu.add(panItem);

        JMenuItem zoomFreeItem = new JMenuItem("自由缩放");
        zoomFreeItem.addActionListener(e -> {
            popupMenu.setVisible(false);
            mapControl.setAction(Action.ZOOMFREE);
        });
        popupMenu.add(zoomFreeItem);

        JMenuItem viewEntireItem = new JMenuItem("全幅显示");
        viewEntireItem.addActionListener(e -> {
            popupMenu.setVisible(false);
            mapControl.getMap().viewEntire();
        });
        popupMenu.add(viewEntireItem);

        JMenuItem zoomInItem = new JMenuItem("放大");
        zoomInItem.addActionListener(e -> {
            popupMenu.setVisible(false);
            mapControl.setAction(Action.ZOOMIN);
        });
        popupMenu.add(zoomInItem);

        JMenuItem zoomOutItem = new JMenuItem("缩小");
        zoomOutItem.addActionListener(e -> {
            popupMenu.setVisible(false);
            mapControl.setAction(Action.ZOOMOUT);
        });
        popupMenu.add(zoomOutItem);

        Map map = mapControl.getMap();

        JMenuItem saveMapItem = new JMenuItem("保存地图");
        saveMapItem.addActionListener(e -> {
            popupMenu.setVisible(false);
            String name = map.getName();
            Workspace workspace = map.getWorkspace();
            Maps maps = workspace.getMaps();
            if (name.equals(AttrConsts.EMPTY) || name.equals("UntitledMap")) {
                Frame frame = (Frame) mapControl.getRootPane().getParent();
                InputNameDialog mapNameDialog = new InputNameDialog(frame, "保存地图名称", true);
                mapNameDialog.setVisible(true);
                String mapName = mapNameDialog.getInputText();
                mapNameDialog.dispose();
                if (mapName.equals(AttrConsts.EMPTY)) {
                    return;
                }
                map.setName(mapName);
                maps.add(map.getName(), map.toXML(), workspace.getVersion());
                workspaceTree.reload();
            } else {
                maps.setMapXML(map.getName(), map.toXML(), workspace.getVersion());
            }
            workspace.save();
        });
        popupMenu.add(saveMapItem);

        JMenuItem saveAsMapItem = new JMenuItem("另存地图");
        saveAsMapItem.addActionListener(e -> {
            popupMenu.setVisible(false);
            Frame frame = (Frame) mapControl.getRootPane().getParent();
            InputNameDialog mapNameDialog = new InputNameDialog(frame, "另存地图名称", true);
            mapNameDialog.setVisible(true);
            String mapName = mapNameDialog.getInputText();
            mapNameDialog.dispose();
            if (mapName.equals(AttrConsts.EMPTY)) {
                return;
            }
            map.setName(mapName);
            Workspace workspace = map.getWorkspace();
            Maps maps = workspace.getMaps();
            maps.add(map.getName(), map.toXML(), workspace.getVersion());
            workspaceTree.reload();
            workspace.save();
        });
        popupMenu.add(saveAsMapItem);

        JMenuItem outputMapToFileItem = new JMenuItem("地图输出图片");
        outputMapToFileItem.addActionListener(e -> {
            popupMenu.setVisible(false);
            Frame frame = (Frame) mapControl.getRootPane().getParent();
            FileDialog outputFileDialog = new FileDialog(frame, "地图输出图片", FileDialog.SAVE);
            String outputMapToFilePath = Helper.getFilePathFromFileDialog(outputFileDialog, AttrConsts.OUTPUT_PNG_FILTER);
            if (outputMapToFilePath == null) {
                return;
            }
            Rectangle2D rectangle2D = map.getViewBounds();
//            Rectangle2D rectangle2D = new Rectangle2D(116.182246, 39.751126, 116.565458, 40.027239);
            long startTime = System.currentTimeMillis();
            boolean flag = map.outputMapToFile(outputMapToFilePath, ImageType.TIFF, 96, rectangle2D, false);
            long endTime = System.currentTimeMillis();
            if (!flag) {
                JOptionPane.showMessageDialog(frame, "地图输出图片失败！", "地图输出图片", JOptionPane.ERROR_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(frame, "地图输出图片成功。出图耗时：" + (endTime - startTime) + " ms", "地图输出图片", JOptionPane.INFORMATION_MESSAGE);
            }
        });
        popupMenu.add(outputMapToFileItem);

        popupMenu.setVisible(true);
    }
}
